package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.message.ChatJson;
import com.drinks.yiniang.entity.message.MessageJson;
import com.drinks.yiniang.entity.message.SendChatMsgJson;
import com.drinks.yiniang.entity.shop.ShopJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 消息api
 * Created by han on 2019/3/6 0006.
 */

public class MessageApiIO extends BaseApiIO {
    volatile static MessageApiIO instance;

    static public MessageApiIO getInstance() {
        if (instance == null) {
            synchronized (MessageApiIO.class) {
                if (instance == null) {
                    instance = new MessageApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 获取消息列表
     *
     * @param type     类型 1 商品 2通知 3消息（私信）
     * @param page     页码
     * @param callback
     */
    public void getMessageList(String type, int page, final APIRequestCallback<MessageJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("type", type);
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MESSAGE_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取消息列表===onSuccess");

                MessageJson mDataJson = JSON.parseObject(res, MessageJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 设置消息为已读
     *
     * @param msgId    消息id （-1全部已读）
     * @param callback
     */
    public void setMessageRead(String msgId, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        paramsMap.put("message_id", msgId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MESSAGE_SET_READ, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("设置消息为已读===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 获取未读消息总数
     *
     * @param callback
     */
    public void getTotalUnReadMessag(final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_GET_TOTAL_UNREAD_MESSAGE, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("设置消息为已读===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 私信消息列表
     *
     * @param fansId   查询消息的 用户ID 对应列表的relation_fans_id
     * @param page     页码
     * @param callback
     */
    public void getMessageChatList(String fansId, int page, final APIRequestCallback<ChatJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("chat_fans_id", fansId);
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MESSAGE_PRIVATE_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("私信消息列表===onSuccess");

                ChatJson mDataJson = JSON.parseObject(res, ChatJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 发送私信消息
     *
     * @param fansId   消息接收方id
     * @param content  内容
     * @param callback
     */
    public void sendPrivateMessage(String fansId, String content, final APIRequestCallback<SendChatMsgJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        paramsMap.put("chat_fans_id", fansId);
        paramsMap.put("content", content);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MESSAGE_SEND_TO_USER, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("发送私信消息===onSuccess");

                SendChatMsgJson mDataJson = JSON.parseObject(res, SendChatMsgJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 客服消息列表
     *
     * @param page     页码
     * @param callback
     */
    public void getCustomerServiceMsgList(int page, final APIRequestCallback<ChatJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MESSAGE_CUSTOMER_SERVICE_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("客服消息列表===onSuccess");

                ChatJson mDataJson = JSON.parseObject(res, ChatJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 发送客服消息
     *
     * @param content  内容
     * @param callback
     */
    public void sendCustomerServiceMsg(String content, final APIRequestCallback<SendChatMsgJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        paramsMap.put("content", content);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MESSAGE_SEND_TO_CUSTOMER_SERVICE, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("发送客服消息===onSuccess");

                SendChatMsgJson mDataJson = JSON.parseObject(res, SendChatMsgJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
